﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

using C1.Phone.Chart;

namespace ControlExplorer.Controls.Chart
{
  public partial class LoadAnimation : UserControl
  {
    Random rnd = new Random();
    ChartType[] chartTypes = new ChartType[] 
      { 
        ChartType.Column, ChartType.ColumnStacked, ChartType.Bar, ChartType.BarStacked,
        ChartType.AreaStacked, ChartType.StepArea,
        ChartType.LineSymbols, ChartType.XYPlot, ChartType.Pie, ChartType.PieExplodedDoughnut
      };


    public LoadAnimation()
    {
      InitializeComponent();

      NewData();
      chart.Tap += (s, e) => NewData();
    }

    void NewData()
    {
      chart.BeginUpdate();

      chart.Data.LoadAnimation = AnimationHelper.CreateAnimation(
          (AnimationTransform)rnd.Next(0,2),
          (AnimationOrigin)rnd.Next(0,2),
          (Easing)rnd.Next(0,12),
          true
        );

      int nser = rnd.Next(2, 4);
      int npts = rnd.Next(4, 8);

      chart.Data.Children.Clear();
      for (int i = 0; i < nser; i++)
        chart.Data.Children.Add(CreateDataSeries(npts));

      chart.ChartType = chartTypes[rnd.Next(0, chartTypes.Length)];
      chart.EndUpdate();
    }

    public DataSeries CreateDataSeries(int npts, bool invert = false)
    {
      var ds = new XYDataSeries() { SymbolSize = new Size(20, 20) };

      int cnt = npts;
      var x = new double[cnt];
      var y = new double[cnt];

      for (int i = 0; i < cnt; i++)
      {
        x[i] = i; y[i] = rnd.NextDouble();
      }

      if (invert)
      {
        ds.XValuesSource = y; ds.ValuesSource = x;
      }
      else
      {
        ds.XValuesSource = x; ds.ValuesSource = y;
      }
      return ds;
    }
  }
}
